var grid ;
var par = new Object();
par.bankRequestType = bankRequestType;
$(function () {
    //Date range picker
    $('#tradeRange').daterangepicker({
        timePicker: false, timePickerIncrement: 30, autoUpdateInput: false, format: 'YYYY-MM-DD h:mm A', locale: {
            applyLabel: '确认',
            cancelLabel: '取消',
            fromLabel: '起始时间',
            toLabel: '结束时间',
            customRangeLabel: '自定义',
            firstDay: 1,
            format: 'YYYY-MM-DD'
        },
        ranges: {
            '今日': [moment().startOf('day'), moment()],
            '昨日': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
            '最近7日': [moment().subtract('days', 6), moment()],
            '最近30日': [moment().subtract('days', 29), moment()],
            '本月': [moment().startOf("month"), moment().endOf("month")],
            '上个月': [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")]
        },
        opens: 'right',    // 日期选择框的弹出位置
        separator: '至',
        showWeekNumbers: true,     // 是否显示第几周
        format: 'YYYY-MM-DD'

    }, function (start, end, label) { // 格式化日期显示框
        var show = start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD');
        $('#tradeRange').val(show);
    });

    //Date range picker
    $('#sendRange').daterangepicker({
        timePicker: false, autoUpdateInput: false, locale: {
            applyLabel: '确认',
            cancelLabel: '取消',
            fromLabel: '起始时间',
            toLabel: '结束时间',
            customRangeLabel: '自定义',
            firstDay: 1,
            format: 'YYYY-MM-DD'
        },
        ranges: {
            '今日': [moment().startOf('day'), moment()],
            '昨日': [moment().subtract('days', 1).startOf('day'), moment().subtract('days', 1).endOf('day')],
            '最近7日': [moment().subtract('days', 6), moment()],
            '最近30日': [moment().subtract('days', 29), moment()],
            '本月': [moment().startOf("month"), moment().endOf("month")],
            '上个月': [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")]
        },
        opens: 'right',    // 日期选择框的弹出位置
        separator: '至',
        showWeekNumbers: true,     // 是否显示第几周
        format: 'YYYY-MM-DD'

    }, function (start, end, label) {
        var show = start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD');
        $('#sendRange').val(show);
    });
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({timePicker: true, timePickerIncrement: 30, format: 'MM/DD/YYYY h:mm A'});
    //Date range as a button
    $('#daterange-btn').daterangepicker(
        {
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            startDate: moment().subtract(29, 'days'),
            endDate: moment()
        },
        function (start, end) {
            $('#daterange-btn span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        }
    );
    $('#sendBtn').click(function () {
        $(".minimal-red:checked").each(function (index, item) {
            var data = $(item).attr("data");
            if (data) {
                var item = JSON.parse(data);
                if (item.bankRequestState != '0') {
                    alert("请选择未发送！");
                    return
                }
            }
        });
    });
    $('#exportBtn').click(function () {
        var selectId = "";
        $(".minimal-red:checked").each(function (index, item) {
            var data = $(item).attr("data");
            if (data) {
                var item = JSON.parse(data);
                selectId +=item.id+",";
            }

        });
        if(selectId=='' || selectId.length<1){
            return;
        }else{
            selectId = selectId.substr(0 , selectId.length -1);
        }
        window.open(queryPath+"/bankOrderExport.do?selectId="+selectId+"&&bankRequestType="+bankRequestType);
    });
    $('#delBtn').click(function () {
        var selectId = "";
        $(".minimal-red:checked").each(function (index, item) {
            var data = $(item).attr("data");
            if (data) {
                var item = JSON.parse(data);
                if(item.bankRequestState =='1' || item.bankRequestState =='2'){
                    alert("已经发送不能删除");
                    return ;
                }
                selectId +=item.id+";";
            }

        });
        if(selectId=='' || selectId.length<1){
            return;
        }
        $.ajax({
            type: "GET",
            url: queryPath+"/deleteBankOrderRequest.do",
            data: {id:selectId},
            dataType: "json",
            async: false,
            success: function(msg) {
                if(msg.success){
                    grid.fnDraw();
                }else{

                }
            }
        });


    });

    $("#queryBtn").click(function () {
        grid.fnSettings().ajax.data = getQueryObj();
        grid.fnDraw();
    });

    $("#resetBtn").click(function () {
        makeClean();
    });

    grid = $("#grid").dataTable({
        paging: true,//分页
        serverSide: true,
        searching: false,
        bLengthChange: false,
        ordering: false,
        responsive: true,
        ajax: {
            url: queryPath+"/bankOrderListRequest.do",
            data: par,
            dataSrc: function (data) {
                $.extend(data, {
                    iTotalDisplayRecords: data.total,
                    iTotalRecords: data.total
                });
                return data.rows;
            }
        },
        columnDefs: [
            {
                defaultContent: "",
                targets: "_all"
            }
        ],
        columns: [
            {
                data: "a1",
                render: function (data, type, full, meta) {
                    var data = JSON.stringify(full);
                    return "<label class=\"\"><input type='checkbox'  class='minimal-red'  data='" + data + "'></label>";
                }
            },

            {data: "requestNo"},
            {
                data: "requestType", render: function (data, type, full, meta) {
                if (data == '0') {
                    return "查询";
                } else {
                    return "拒付";
                }
            }
            },
            {
                data: "bankRequestNo"
            },
            {
                data: "tradeDate", render: function (data, type, full, meta) {
                if (data != '' && data.length > 10) {
                    data = data.substr(0, 10);
                }
                return data;
            }
            },
            {data: "tradeMoney"},
            {
                data: "interceptState", render: function (data, type, full, meta) {
                if (data == '1') {
                    return "是";
                } else {
                    return "否";
                }

            }
            },
            {
                data: 'emergencyState', render: function (data, type, full, meta) {
                if (data == '1') {
                    return "紧急";
                } else {
                    return "普通";
                }
            }
            },
            {data: "bankAttachCount"},
            {data: "jdAttachCount"},
            {
                data: "bankRequestState", render: function (data, type, full, meta) {
                if (data == '0') {
                    return "未发送";
                } else if (data == '1') {
                    return "未回复";
                } else {
                    return "已回复";
                }

            }
            },
            {
                data: "sendTime", render: function (data, type, full, meta) {
                if (data && data != '' && data.length > 10) {
                    data = data.substr(0, 10);
                }
                return data;
            }
            },
            {
                data: "realName", render: function (data, type, full, meta) {
                return "<a href=\"#\" onclick=\"detail('" + full.bankRequestNo + "','" + full.bankRequestState + "')\">详情</a>"
            }
            }
        ],
        language: {
            lengthMenu: '<select class="form-control input-xsmall">' + '<option value="1">1</option>' + '<option value="10">10</option>' + '<option value="20">20</option>' + '<option value="30">30</option>' + '<option value="40">40</option>' + '<option value="50">50</option>' + '</select>条记录',//左上角的分页大小显示。
            paginate: {//分页的样式内容。
                previous: "上一页",
                next: "下一页",
                first: "第一页",
                last: "最后"
            },

            zeroRecords: "没有内容",//table tbody内容为空时，tbody的内容。
            //下面三者构成了总体的左下角的内容。
            info: "总共_PAGES_ 页，显示第_START_ 到第 _END_ ，筛选之后得到 _TOTAL_ 条，初始_MAX_ 条 ",//左下角的信息显示，大写的词为关键字。
            infoEmpty: "0条记录",//筛选为空时左下角的显示。
            infoFiltered: ""//筛选之后的左下角筛选提示，
        },
        paging: true,
        pagingType: "full_numbers",//分页样式的类型

    });

    if(bankRequestType == '0')
    {
        $(".bankRequestTypeCls").each(function (index,item) {
           $(item).html('银行订单号');
        }) ;
    }else{
        $(".bankRequestTypeCls").each(function (index,item) {
            $(item).html('银行卡号');
        }) ;
    }

});
    function addBankOrderReq()
    {
        window.location.href = queryPath+"/toAddBankOrderRequestPage.do";
    }

    function batchBankOrderReq() {
        window.open( queryPath+"/toBatchBankOrderRequestPage.do","_batchI","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes");
    }

function detail(bankRequestNo ,bankRequestState){
    if(bankRequestState == '0')
    {
        window.location.href =queryPath+ "/toBankOrderDetailRequestModifyPage.do?bankRequestNo="+bankRequestNo;
    }else{
        window.location.href = queryPath+"/toBankOrderDetailRequestPage.do?bankRequestNo="+bankRequestNo;
    }
}

function getQueryObj() {
    par = new Object();
    par.bankRequestType = bankRequestType;
    if($('#bankRequestNo').val().length >0){
        par.bankRequestNo = $('#bankRequestNo').val();
    }
    if($('#requestType').val().length >0){
        par.requestType = $('#requestType').val();
    }
    if($('#emergencyState').val().length >0)
    {
        par.emergencyState =$('#emergencyState').val();
    }
    if($('#interceptState').val().length>0){
        par.interceptState = $('#interceptState').val();
    }
    if($('#bankRequestState').val().length>0){
        par.bankRequestState = $('#bankRequestState').val();
    }
    if($('#bankAttachCount').val().length>0){
        par.bankAttachCount = $('#bankAttachCount').val();
    }
    if($('#jdAttachCount').val().length >0){
        par.jdAttachCount =$('#jdAttachCount').val();
    }
    var tradeRange = $('#tradeRange').val();
    if(tradeRange && tradeRange.length >0)
    {
        var tradeSeg = tradeRange.split(" - ");
        if(tradeSeg[0] == tradeSeg[1]){
            par.tradeStart = tradeSeg[0]+" 00:00:00";
            par.tradeEnd = tradeSeg[0]+" 59:59:59";
        }else{
            par.tradeStart = tradeSeg[0]+" 00:00:00";
            par.tradeEnd = tradeSeg[1]+" 59:59:59";
        }
    }
    var sendRange = $('#sendRange').val();
    if(sendRange && sendRange.length>0)
    {
        var sendSeg = sendRange.split(" - ");
        if(sendSeg[0] == sendSeg[1])
        {
            par.sendStart = sendSeg[0]+" 00:00:00";
            par.sendEnd = sendSeg[0]+" 59:59:59";
        }else{
            par.sendStart = sendSeg[0]+" 00:00:00";
            par.sendEnd = sendSeg[1]+" 59:59:59";
        }
    }
    return par;
}

function makeClean()
{
    $('input').each(function (index ,item) {
        $(this).val('');
    })
    $('select').each(function (index, item) {
        $(this).val('');
    });
    $('textarea').each(function () {
        $(this).val('');
    })
    $('#fileListDiv').empty();
}

// var par = new Object();
//  par.start = 0;
//  par.limit = 20;
// par.curr = 1;
function loadGrid(par)
{
    $.ajax({
        type: "GET",
        url: "${basePath}/bank/bankOrderRequest/bankOrderListRequest.do",
        data: par,
        dataType: "json",
        async: false,
        success: function(msg) {
            if(msg.success){
                var html = template('grid_tmp', msg);
                document.getElementById('grid').innerHTML = html;
                par.total = msg.total;
                $('.curr').html(par.curr);
                $('.tol-pag').html("共"+msg.total+"条");
            }else{

            }
        }
    });
}
